﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.Packet;
using System.Net;

namespace Hi_Baidu.FileShare.Packet.InPacket
{
    public class AllowSendFilePacket : SendFileInPackage
    {
        public AllowSendFilePacket(InPackage inpackage)
            : base(inpackage)
        {
        }

        public override void InitPackage()
        {
            int Offset = 0;
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            byte[] FileSessionLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FileSessionLength, 0, 4);
            Offset += 4;
            byte[] FileSession = new byte[BitConverter.ToInt32(FileSessionLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FileSession, 0, FileSession.Length);
            Offset += FileSession.Length;
            byte[] IPLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPLength, 0, 4);
            Offset += 4;
            byte[] IPAddress = new byte[BitConverter.ToInt32(IPLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPAddress, 0, IPAddress.Length);
            Offset += IPAddress.Length;
            byte[] FilePort = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FilePort, 0, 4);
            Offset += 4;

            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("FILEPORT", FilePort);
            this.DIC_CONTENT.Add("FILESESSION", FileSession);
            this.DIC_CONTENT.Add("IPADDRESS", IPAddress);
        }

        /// <summary>
        ///     获取文件传送会话ID
        /// </summary>
        public String GetFileSession()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["FILESESSION"]);
        }

        /// <summary>
        ///     获得文件端口
        /// </summary>
        public int GetFilePort()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["FILEPORT"], 0);
        }

        /// <summary>
        ///     获得IP地址
        /// </summary>
        public IPAddress GetIPAddress()
        {
            return IPAddress.Parse(Encoding.Default.GetString(this.DIC_CONTENT["IPADDRESS"]));
        }
    }
}
